package org.aurora.one.threadlocal_5;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Hashtable;

/**
 * @author : Aurora
 * @date : 2024/3/1 14:51
 * @description 测试ThreadLocal
 */
public class ThreadLocalTest {
    //1、创建线程变量
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    private static InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) {
        //2、设置线程变量
//        threadLocal.set("hello world");
        inheritableThreadLocal.set("hello world");
        //3、启动子线程
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //4、子线程输出线程变量值
//                System.out.println("thread: " + threadLocal.get());
                System.out.println("thread: " + inheritableThreadLocal.get());
            }
        });
        thread.start();
        //5、主线程输出线程变量的值
//        System.out.println("main:"+threadLocal.get());
        System.out.println("main:"+inheritableThreadLocal.get());
        //输出结果(threadLocal)：
        //main:hello world
        //thread: null

        //输出结果(inheritableThreadLocal)：
        //main:hello world
        //thread: hello world
    }
}
